<?php
    @session_start();
require_once 'shared-functions.php';
require_once 'session.php';
require_once 'masterpage.php';

if (!IsValidSession()) {
    header('Location: login.php?page=student-billing-history');
    exit();
}
else
{
    RefreshSession();
}

$user = GetCurrentUserAccessLevel();

//Check user access
if ($user != $DIRECTOR && $user != $ADMIN && $user != $REGISTRAR) {
    header('Location: login.php?page=student-billing-history&error=To access the student billing history page, please log in as a director, admin or registrar&logout=1');
    exit();
}

if (!isset($_POST['StudentID'])) {
    header('Location: student-selector.php?page=student-billing-history');
    exit();
}


$link = connect_db();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" type="text/css" href="master.css" />
	<title>MLHTS - Student Billing History</title>
	</head>
	<body>

<table class="DataList" border="1">
    <tr>
        <th colspan="5">Payment History</th>
    </tr>
    <tr>
        <th>Date</th>
        <th>Transaction Type</th>
        <th>Amount</th>
        <th>User</th>
        <th>Notes</th>
    </tr>
<?php
        $query = "SELECT `FeeLedger`.`Date`, 
                         `FeeLedger`.`Amount`,
                         `FeeLedger`.`ProcessingUser`,
                         `FeeLedger`.`Comment`,
                         `feeTypes`.`description`
                         FROM `FeeLedger`,`feeTypes` WHERE `StudentID`='" . $_POST['StudentID'] . "' AND `FeeLedger`.`Type`=`feeTypes`.`type` ORDER BY `Date` ASC;";
    $result = mysql_query($query, $link);
    $accountBalance = 0;
    while ($ledgerEntry = mysql_fetch_array($result, MYSQL_BOTH))
    {
        $accountBalance += $ledgerEntry['Amount'];
        echo '<tr>';
        echo '<td>' . date('d-m-Y H:i:s', strtotime($ledgerEntry['Date'])) . "</td>";
        echo '<td>' . $ledgerEntry['description'] . "</td>";
        echo '<td>' . $ledgerEntry['Amount'] . "</td>";
        echo '<td>' . $ledgerEntry['ProcessingUser'] . "</td>";
        echo '<td>' . $ledgerEntry['Comment'] . "</td>";
        echo '</tr>';
    }
    echo '<tr><td></td><td>Current Balance</td><td>' . number_format($accountBalance, 2) . ' GH&cent;</td><td></td><td></td></tr>';
    ?>
</table>
</body>
</html>